#pragma once
#include "Article.h"
#include <unordered_map>
#include <fstream>
#include <iostream>

class ArticleRegister
{
private:
	unordered_map<int,string> AR;
public:

	ArticleRegister()
	{
	}

	friend ostream& operator<<(ostream& ostream, ArticleRegister& AR)
	{
		ostream << "MENU FOR ARTICLEREGISTER" << endl;
		ostream << "1. Add article" << endl;
		ostream << "2. Delete article" << endl;
		ostream << "3. Change an articles data" << endl;
		ostream << "4. Search for article" << endl;
		ostream << "5. Print register" << endl;
		ostream << "6. Exit" << endl;
		return ostream;
	}

	friend istream& operator>>(istream& istream, ArticleRegister& AR)
	{
		cout << "Answer: ";

		string answer;
		istream >> answer;

		if (answer == "1")
		{
			AR.AddArticle();
		}

		else if (answer == "2")
		{
			AR.DeleteArticle();
		}

		else if (answer == "3")
		{
			AR.ChangeData();
		}

		else if (answer == "4")
		{
			AR.SearchArticle();
		}

		else if (answer == "5")
		{
			AR.PrintRegister(cout);
		}

		else if (answer == "6")
		{
			AR.WriteToFile("Articleregister.txt");
			exit(EXIT_SUCCESS);
		}

		return istream;
	}

	void ReadFromFile(string fileName)
	{
		string str, strRow;
		ifstream fin;

		try
		{
			fin.open(fileName);
			AR.clear();

			Article a;

			while (getline(fin, strRow))
			{
				istringstream iss(strRow);

				if (a.GetID() != -1)
				{
					AR.insert(make_pair<int, string>(a.GetID(), a.GetName()));
				}

				a = Article();
				iss >> a;
			}

			if (a.GetID() != -1)
			{
				AR.insert(make_pair<int, string>(a.GetID(), a.GetName()));
			}

			fin.close();
		}

		catch (exception e)
		{
			cerr << "Exception: " << fileName << endl;
			cerr << e.what() << endl;
		}
	}
	
	void WriteToFile(string fileName)
	{
		ofstream fout;
		fout.open(fileName);
		PrintRegister(fout);
		fout.close();
	}

	void AddArticle()
	{
		Article temp;

		cout << endl << "Add Article" << endl;
		cout << "(id,name)" << endl;
		cin >> temp;
		AR.insert(make_pair<int, string>(temp.GetID(), temp.GetName()));
	}

	void DeleteArticle()
	{
		cout << endl << "Delete Article" << endl;
		cout << "ID: " << endl;
		int IDtemp;
		cin >> IDtemp;
		AR.erase(IDtemp);
	}

	void ChangeData()
	{
		cout << endl << "Change" << endl;
		cout << "ID: ";
		int tempID;
		cin >> tempID;
		unordered_map<int, string>::const_iterator got = AR.find(tempID);

		if (got == AR.end())
		{
			cout << "Not found" << endl;
		}

		else
		{
			cout << "Edit name to: ";
			string tempName;
			getline(cin, tempName);

			pair<int, string> tempPair(tempID, tempName); // FIXAAAAAAAAA 

			
		}
		

	}

	void SearchArticle()
	{
		cout << endl << "Search Article" << endl;
		cout << "ID: ";
		int IDtemp;
		cin >> IDtemp;
		unordered_map<int, string>::const_iterator got = AR.find(IDtemp);

		if (got == AR.end())
		{
			cout << "Not found" << endl;
		}

		else
			cout << got->second << endl;
	}

	void PrintRegister(ostream &out)
	{
		for (auto it = AR.begin(); it != AR.end(); it++)
		{
			out << "(" << it->first << "," << it->second << ")";
			out << endl;
		}
	}

	~ArticleRegister()
	{
	}
};

